package com.myaihui.controller;

import com.myaihui.config.FileConfig;
import com.myaihui.domain.Banner;
import com.myaihui.foundation.msg.ResultMsg;
import com.myaihui.service.BannerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by sun on 2018/7/6.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@RequestMapping("/banners")
@Controller
public class BannerController {
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Static fields/constants/initializer
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    private static Logger log = LoggerFactory.getLogger(BannerController.class);

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Instance fields
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileConfig fileConfig;

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Constructors
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */



    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Public Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @RequestMapping({"/", ""})
    public String home(Model model) {
        String prefix = fileConfig.getUrl();
        if (prefix.endsWith(File.separator)) {
            prefix = prefix.substring(0, prefix.length() - 1);
        }
        List<Banner> pics = bannerService.queryAll();
        for (Banner pic : pics) {
            pic.setUrl(prefix + pic.getUrl());
        }
        model.addAttribute("pics", pics);
        return "banner/home";
    }

    @RequestMapping(value = "/deletePic")
    @ResponseBody
    public ResultMsg deletePic(String id) {
        bannerService.delete(id);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "delete");
        return ResultMsg.success(rtn);
    }

    @RequestMapping(value = "/updateTarget")
    @ResponseBody
    public ResultMsg updateTarget(String id, String target) {
        bannerService.update(id, target, null);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "target");
        return ResultMsg.success(rtn);
    }

    @RequestMapping(value = "/updateRank")
    @ResponseBody
    public ResultMsg updateRank(String id, Integer rank) {
        bannerService.update(id, null, rank);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "rank");
        return ResultMsg.success(rtn);
    }

    @RequestMapping(value = "/createPic")
    @ResponseBody
    public ResultMsg createPic(@RequestParam("pic") MultipartFile pic, String target, String activeName) throws IOException {
        String saveDir = fileConfig.getFileSaveDir();
        String bannerDirName = "banner";
        String savePath = saveDir + bannerDirName;
        File dir = new File(savePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + pic.getOriginalFilename();
        File saveFile = new File(savePath + "/" + fileName);
        pic.transferTo(saveFile);

        String url = String.format("%s%s", "/", bannerDirName + "/" + fileName);
        bannerService.insert(url, target, null, activeName);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "insert");
        return ResultMsg.success(rtn);
    }

    @RequestMapping(value = "/hidePic")
    @ResponseBody
    public ResultMsg hidePic(String id) {
        bannerService.doHide(id);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "hide");
        return ResultMsg.success(rtn);
    }

    @RequestMapping(value = "/showPic")
    @ResponseBody
    public ResultMsg showPic(String id) {
        bannerService.doShow(id);
        Map<String, String> rtn = new HashMap<>(1);
        rtn.put("type", "show");
        return ResultMsg.success(rtn);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Private Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


}